using System.Globalization;
using Admin2025.Application.common;
using Admin2025.Application.Dtos;
using Admin2025.Domain.Entities.App;
using Admin2025.Domain.Repository;

namespace Admin2025.Application.Services;

public class AppPermissionService : IAppPermissionService
{
    private readonly IRepository<AppPermission> _permissionRepo;
    public AppPermissionService(IRepository<AppPermission> permissionRepo)
    {
        _permissionRepo = permissionRepo;
    }

     public async Task<dynamic> GetAllPermissionAsync()
    {
         var list = await _permissionRepo.GetAllAsync();
            return list.Select(user => new PermissionDto
            {
                Id = user.Id,
                // PermissionCode = user.PermissionCode,
                PermissionName=user.PermissionName
            });
    }
    public async Task<dynamic> CreatePermissionAsync(CreatePermissionDto dto)
    {
        var isExists = _permissionRepo.Table.Any(x => x.Code == dto.Code);
        if (isExists)
        {
            return ApiResult.Fail(3002, "已存在同名权限，请确认重试");
        }
        var permission = new AppPermission { Code = dto.Code, PermissionName = dto.PermissionName };
        await _permissionRepo.CreateAsync(permission);
        return ApiResult.Success(permission, "创建权限成功");
    }
    public async Task<dynamic> DeletePermissionAsync(Guid permissionId)
    {
        var permission = await _permissionRepo.GetByIdAsync(permissionId);
        if (permission == null)
        {
            return ApiResult.Fail(4004, "要删除的权限不存在，请确认后重试");
        }
        if (permission.IsDeleted)
        {
            return ApiResult.Fail(4004, "要删除的权限已被删除，请确认后重试");
        }
        permission.IsDeleted = true;
        await _permissionRepo.UpdateAsync(permission);
        return ApiResult.Success(permission, "删除权限成功");
    }
    public async Task<dynamic> UpdatePermissionAsync(Guid permissionId, UpdatePermissionDto dto)
    {
        var permission = await _permissionRepo.GetByIdAsync(permissionId);
        if (permission == null)
        {
            return ApiResult.Fail(4004, "要修改的权限不存在，请确认后重试");
        }
        if (permission.IsDeleted)
        {
            return ApiResult.Fail(4004, "要修改的权限已被删除，请确认后重试");
        }
        if (!permission.IsActive)
        {
            return ApiResult.Fail(4004, "要修改的权限未启用，请确认后重试");
        }
        permission.PermissionName = dto.PermissionName;
        await _permissionRepo.UpdateAsync(permission);
        return ApiResult.Success(permission, "修改权限成功");
    }
    public async Task<dynamic> EnablePermission(Guid permissionId)
    {
        var permission = await _permissionRepo.GetByIdAsync(permissionId);
        if (permission == null)
        {
            return ApiResult.Fail(4004, "权限不存在，请确认后重试");
        }
        if (permission.IsDeleted)
        {
            return ApiResult.Fail(4004, "权限已被删除，请确认后重试");
        }
        if (permission.IsActive)
        {
            return ApiResult.Fail(4004, "权限已启用，无需重复启用");
        }
        permission.IsActive = true;
        await _permissionRepo.UpdateAsync(permission);
        return ApiResult.Success(permission, "启用成功");
    }
    public async Task<dynamic> DisablePermission(Guid permissionId)
    {
        var permission = await _permissionRepo.GetByIdAsync(permissionId);
        if (permission == null)
        {
            return ApiResult.Fail(4004, "权限不存在，请确认后重试");
        }
        if (permission.IsDeleted)
        {
            return ApiResult.Fail(4004, "权限已被删除，请确认后重试");
        }
        if (!permission.IsActive)
        {
            return ApiResult.Fail(4004, "权限已禁用，无需重复禁用");
        }
         permission.IsActive = false;
        await _permissionRepo.UpdateAsync(permission);
        return ApiResult.Success(permission, "禁用成功");
    }
}